package com.fishbone.cloud.auth.security.config;

import com.fishbone.cloud.auth.constants.FishBoneConstant;
import com.fishbone.cloud.auth.security.handler.FishBoneAuthenticationFailureHandler;
import com.fishbone.cloud.auth.security.handler.FishBoneAuthenticationSuccessHandler;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @ClassName WebSecurity
 * @Author huyaxi
 * @DATE 2019/8/26 13:42
 */
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
@AllArgsConstructor
public class FishBoneSecurity extends WebSecurityConfigurerAdapter {

    private final FishBoneAuthenticationSuccessHandler successHandler;

    private final FishBoneAuthenticationFailureHandler failureHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic().and().csrf().disable();
        http.formLogin().loginPage(FishBoneConstant.LOGIN_PAGE)
                .loginProcessingUrl(FishBoneConstant.LOGIN_PROCESS_URL)
                .successHandler(successHandler)
                .failureHandler(failureHandler);
    }


    @Bean
    public PasswordEncoder passwordEncoder() {
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }


    @Bean
    @Override
    @SneakyThrows
    public AuthenticationManager authenticationManagerBean() {
        return super.authenticationManagerBean();
    }

}
